package com.xxxx.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xxxx.server.pojo.Admin;
import com.xxxx.server.pojo.Menu;
import com.xxxx.server.mapper.MenuMapper;
import com.xxxx.server.service.IMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhoubin
 * @since 2022-04-13
 */
@Service("m")
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements IMenuService {
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;
    @Autowired
    private MenuMapper menuMapper;
    @Override
    public List<Menu> getMenusByAdminId() {
        Integer adminId =((Admin)SecurityContextHolder.getContext()
                .getAuthentication().getPrincipal()).getId();
    List<Menu> menus= (List<Menu>) redisTemplate.opsForValue().get("menu_"+adminId);
    if (CollectionUtils.isEmpty(menus)){
        menus=menuMapper.getMenusByAdminId(adminId);
        redisTemplate.opsForValue().set("menu_"+adminId,menus,60, TimeUnit.SECONDS);
    }
        return menus;
    }

    @Override
    public List<Menu> getMenusWithRole() {
        return menuMapper.getMenusWidthRole();
    }

    @Override
    public List<Menu> getAllMenus() {
        return menuMapper.getAllMenus();
    }

}
